package ${package.ServiceImpl};

import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
<#if table.serviceInterface>
import ${package.Service}.${table.serviceName};
</#if>
import ${superServiceImplClassPackage};
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.util.Assert;
import com.settlement.system.common.util.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.model.query.${entity}Query;
import com.settlement.system.model.form.${entity}Form;
import org.springframework.beans.BeanUtils;
import com.settlement.system.model.vo.${entity}ImportVo;

/**
 * <p>
 * ${table.comment!} 服务实现类
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@Service
<#if kotlin>
open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>()<#if table.serviceInterface>, ${table.serviceName}</#if> {

}
<#else>
public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}><#if table.serviceInterface> implements ${table.serviceName}</#if> {

    @Override
    public Page<${entity}> getPage(${entity}Query queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<${entity}> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getPage(page, queryParams);
    }

     @Override
     public boolean save${entity}(${entity}Form ${entity?uncap_first}Form) {
         ${entity} ${entity?uncap_first} = new ${entity}();
         BeanUtils.copyProperties(${entity?uncap_first}Form, ${entity?uncap_first});
         ${entity?uncap_first}.setCreateTime(null);
         return this.saveOrUpdate(${entity?uncap_first});
     }

     @Override
     public ${entity}Form get${entity}FormData(Long id) {
         ${entity} ${entity?uncap_first} = this.getById(id);
         ${entity}Form ${entity?uncap_first}Form = new ${entity}Form();
         BeanUtils.copyProperties(${entity?uncap_first}, ${entity?uncap_first}Form);
         return ${entity?uncap_first}Form;
     }

     @Override
     public boolean delete${entity}(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
     }

    @Override
    public void importData(List<${entity}ImportVo> voList)  {
        List<${entity}> ${entity?uncap_first}List = new ArrayList<>();
        for (${entity}ImportVo importVo : voList) {
            ${entity} ${entity?uncap_first} = new ${entity}();
            BeanUtils.copyProperties(importVo, ${entity?uncap_first});
            ${entity?uncap_first}List.add(${entity?uncap_first});
        }
        this.saveOrUpdateBatch(${entity?uncap_first}List);
    }

    @Override
    public List<${entity}ImportVo> findByIds(String idsStr) {
        if (!StringUtils.hasText(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<${entity}> ${entity?uncap_first}List = this.getBaseMapper().selectBatchIds(ids);
        List<${entity}ImportVo> voList = new ArrayList<>();
        for (${entity} ${entity?uncap_first} : ${entity?uncap_first}List) {
            ${entity}ImportVo vo = new ${entity}ImportVo();
            BeanUtils.copyProperties(${entity?uncap_first}, vo);
            voList.add(vo);
        }
        return voList;
    }
}
</#if>
